package com.decent.common.bo;

import com.decent.common.enums.AuthExceptionEnum;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.common.exception.ErrorCodeException;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 接口返回信息
 *
 * @author wangyan
 */
@Data
@NoArgsConstructor
public class MessageBean implements Serializable {
    private static final long serialVersionUID = 7192766535561421181L;
    private String message;
    private Object data;
    private Integer code;

    public MessageBean(ErrorCodeEnum code, Object data, String message) {
        this.code = code.getCode();
        this.data = data;
        this.message = message;
    }

    public MessageBean(ErrorCodeEnum code, Object data) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = data;
    }

    public MessageBean(AuthExceptionEnum errorCode) {
        this.code = errorCode.getCode();
        this.message = errorCode.getMessage();
    }

    public MessageBean(ErrorCodeEnum code, String message) {
        this.code = code.getCode();
        this.message = message;
    }

    public MessageBean(ErrorCodeEnum code) {
        this.code = code.getCode();
        this.message = code.getMessage();
    }

    public MessageBean(ErrorCodeException e) {
        this.code = e.getCode();
        this.message = e.getMessage();
    }

    public static MessageBean fail(ErrorCodeEnum code) {
        return new MessageBean(code);
    }

    public static MessageBean fail(String msg) {
        return new MessageBean(ErrorCodeEnum.NO, msg);
    }

    public static MessageBean fail() {
        return fail("操作失败");
    }

    public static MessageBean success(Object data, String msg) {
        return new MessageBean(ErrorCodeEnum.OK, data, msg);
    }

    public static MessageBean success() {
        MessageBean messageBean = new MessageBean();
        messageBean.setCode(ErrorCodeEnum.OK.getCode());
        messageBean.setMessage("操作成功");
        return messageBean;
    }

    public static boolean isSuccess(MessageBean result) {
        return result != null && ErrorCodeEnum.OK.getCode().equals(result.getCode());
    }

    public static MessageBean success(Object data) {
        return new MessageBean(ErrorCodeEnum.OK, data);
    }

    public static MessageBean success(String msg) {
        return new MessageBean(ErrorCodeEnum.OK, msg);
    }
}